package com.my.gesturelockandwakedemo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.my.gesturelockandwakedemo.activity.BaseActivity;
import com.my.gesturelockandwakedemo.activity.CheckGestureLockActivity;
import com.my.gesturelockandwakedemo.activity.LoginActivity;
import com.my.gesturelockandwakedemo.activity.SettingGestureLockActivity;
import com.my.gesturelockandwakedemo.util.CacheActivity;
import com.my.gesturelockandwakedemo.util.SharedPrefUtil;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private long mExitTime = 0;
    private SharedPrefUtil sp;
    private String hasGesture;
    /***  退出按钮 */
    private Button btnOutLine;
    /*** 登录按钮 */
    private Button btnLogin;
    private String strUserId;
    private String TAG = "mwbm";
    /*** 是否登录 */
    private TextView tvLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = new SharedPrefUtil(this, "MyGestureLock");

        // 1.判断是否为登录状态
        // 1.1 是：判断手势
        // 1.2 否:不进行任何操作
        strUserId = sp.getString("userId", ""); // 第一次打开App
        if (strUserId.isEmpty()) {
            sp.putString("userId", "0");
            sp.commit();
            strUserId = sp.getString("userId", "");
            Log.i(TAG, "if userId：" + strUserId);
        } else if (strUserId.equals("0")) { // 没有登录
            Log.i(TAG, "else if: userId：" + strUserId);
        } else {
            Log.i(TAG, "else: userId：" + strUserId);
            hasCestureLock();
        }
        initViews();
    }

    @Override
    protected void onResume() {
        super.onResume();
        strUserId = sp.getString("userId", "");
        if (strUserId.isEmpty()) {
            tvLogin.setText("未登录");
        } else if (strUserId.equals("0")) {
            tvLogin.setText("未登录");
        } else {
            tvLogin.setText(strUserId);
        }
    }

    @Override
    public void initViews() {
        super.initViews();
        btnOutLine = (Button) findViewById(R.id.btn_out_line);
        btnOutLine.setOnClickListener(this);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        tvLogin = (TextView) findViewById(R.id.tv_login_main);
    }

    /**
     * 是否有手势密码
     */
    private void hasCestureLock() {
        // 1.sp 中找到是否有手势密码
        // 1.1 有，验证
        // 1.2 没有，创建
        hasGesture = sp.getString("MyGesture", "");
        if (hasGesture.isEmpty()) { // 没有手势密码,创建手势密码
            goIntent(SettingGestureLockActivity.class);
        } else { // 有，验证
            goIntent(CheckGestureLockActivity.class);
        }
    }

    private void goIntent(Class cla) {
        Intent intent = new Intent(this, cla);
        startActivity(intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mExitTime) > 2000) {
                //   Object mHelperUtils;
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                mExitTime = System.currentTimeMillis();
            } else {
                CacheActivity.finishActivity();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_out_line: // 退出
                sp.clear();
                CacheActivity.finishActivity();
                break;
            case R.id.btn_login: // 登录 TODO ========================
                strUserId = sp.getString("MyGesture", "");
                if (strUserId.isEmpty()) {
                    goIntent(LoginActivity.class);
                } else if (strUserId.equals("0")) {
                    goIntent(LoginActivity.class);
                } else {
                    Toast.makeText(this, "已登录", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

}
